package org.zhaolei.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.zhaolei.annotation.NoLogin;
import org.zhaolei.entity.auth.AuthUserEntity;
import org.zhaolei.entity.auth.CaptchaEntity;
import org.zhaolei.entity.auth.JwtUserEntity;
import org.zhaolei.entity.auth.TokenEntity;
import org.zhaolei.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Api(tags = "web用户操作", description = "web用户接口")
@RestController
@RequestMapping("/v1/web/user")
@Validated
public class WebUserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param authUserEntity
     * @return
     */
    @NoLogin
    @ApiOperation(value = "用户登录", notes = "用户登录")
    @PostMapping("/login")
    public TokenEntity login(@Valid @RequestBody AuthUserEntity authUserEntity) {
        return userService.login(authUserEntity);
    }

    /**
     * 用户登出
     * @param request
     */
    @NoLogin
    @ApiOperation(value = "用户登出", notes = "用户登出")
    @PostMapping("/logout")
    public void logout(HttpServletRequest request) {
        userService.logout(request);
    }

    @NoLogin
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @GetMapping(value = "/code")
    public CaptchaEntity getCode() {
        return userService.getCode();
    }

    @NoLogin
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping(value = "/info")
    public JwtUserEntity getUserInfo() {
        return userService.getUserInfo();
    }
}
